package com.fulihui.usercore.biz.conv;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.near.toolkit.common.StringUtil;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.fulihui.usercore.condition.ThirdMerchantCondition;
import com.fulihui.usercore.dal.dataobj.ThirdMerchantDO;
import com.fulihui.usercore.dto.ThirdMerchantDTO;
import com.fulihui.usercore.request.merchant.ThirdMerchantSaveRequest;

/**
 *
 * @author Willard.Hu on 2016/9/23 0023.
 */
public class ThirdMerchantConv {

    public static ThirdMerchantDO toDO(ThirdMerchantSaveRequest request, Map<String, Object> old) {
        if (request == null) {
            return null;
        }
        ThirdMerchantDO dataobj = new ThirdMerchantDO();
        dataobj.setId(request.getId());
        dataobj.setName(request.getName());
        dataobj.setBizType(request.getBizType());
        if (!CollectionUtils.isEmpty(request.getExtInfo())) {
            if (old == null) {
                old = new HashMap<>();
            }
            request.getExtInfo().forEach(old::put);
            dataobj.setExtInfo(JSONObject.toJSONString(old));
        }
        return dataobj;
    }

    public static ThirdMerchantDO toDO(ThirdMerchantCondition condition) {
        if (condition == null) {
            return null;
        }
        ThirdMerchantDO dataobj = new ThirdMerchantDO();
        dataobj.setBizType(condition.getBizType());
        dataobj.setName(condition.getName());
        return dataobj;
    }

    public static ThirdMerchantDTO toDTO(ThirdMerchantDO dataobj) {
        if (dataobj == null) {
            return null;
        }
        ThirdMerchantDTO dto = new ThirdMerchantDTO();
        dto.setId(dataobj.getId());
        dto.setName(dataobj.getName());
        dto.setBizType(dataobj.getBizType());
        if (StringUtil.isNotBlank(dataobj.getExtInfo())) {
            dto.setExtInfo(JSONObject.parseObject(dataobj.getExtInfo()));
        }
        return dto;
    }

    public static List<ThirdMerchantDTO> toList(List<ThirdMerchantDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return Collections.emptyList();
        }
        return doList.stream().map(ThirdMerchantConv::toDTO).collect(Collectors.toList());
    }
}
